package cn.bigcontainer.alt.sys.dto;

import java.util.ArrayList;
import java.util.List;

import cn.bigcontainer.alt.sys.po.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;

import cn.bigcontainer.alt.common.entity.BigMap;
import cn.bigcontainer.alt.common.util.WoKit;
import cn.bigcontainer.alt.sys.po.User;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class UserDto extends User {

	public static final String PWD = "123456";

	private List<MenuDto> tops = new ArrayList<MenuDto>();

	/**
	 * 逗号隔开的角色id
	 */
	private List<RoleDto> roles = new ArrayList<RoleDto>();

	private BigMap<String, Object> data = new BigMap<String, Object>();
	
	public UserDto() {
	}

	public UserDto(User po) {
		super(po);
	}

	public String getAliasName2() {
		if (WoKit.isEmpty(aliasName)) {
			return name;
		}
		return aliasName;
	}

	/**
	 * 如果image为空，则给出默认值
	 * @return
	 */
	public String getImage2() {
		if (!WoKit.isEmpty(image) && image.indexOf('/') > 0) {
			return image;
		}
		if (WoKit.isEmpty(image) || "0".equals(image)) {
			return "/dist/img/avatar.png";
		}
		return "/meta/file/" + image;
	}

	public List<MenuDto> getTops(String menu) {
		// 2.设置active
		if (WoKit.isEmpty(menu)) {// 2.1.如果menu为空，设置第一个顶级菜单及其第一个子菜单
			// 判断之前是否设置过active
			Boolean setActive = false;
			for (MenuDto m : tops) {
				if (m.getActive()) {
					setActive = true;
				}
			}
			if (!setActive && tops.size() > 0) {
				tops.get(0).setActive(true);
				if (tops.get(0).getChildren().size() > 0) {
					tops.get(0).getChildren().get(0).setActive(true);
				}
			}
		} else {
			for (MenuDto dto : tops) {// 2.2.设置no为menu的二级菜单及其父菜单为active
				dto.setActive(false);
				for (MenuDto c : dto.getChildren()) {
					if (c.getNo().equals(menu)) {
						c.setActive(true);
						dto.setActive(true);
					} else {
						c.setActive(false);
						// dto.setActive(false);
					}
				}
			}
		}
		return tops;
	}

	public MenuDto getCurrentMenu () {
		for (MenuDto top : tops) {
			for (MenuDto c : top.getChildren()) {
				if (c.getActive()) {
					return c;
				}
			}
		}
		return null;
	}

	@JsonIgnore
	public Boolean isAdmin() {
		return roles.stream().filter(r -> r.getId() == 1).findAny().isPresent();
	}
	
	@JsonIgnore
	public String getRolesName () {
		StringBuffer sBuffer = new StringBuffer();
		roles.forEach(r -> {
			sBuffer.append(",");
			sBuffer.append(r.getName());
		});
		if (sBuffer.length() > 0) {
			return sBuffer.substring(1).toString();
		}
		return "";
	}

	/**
	 * 根据类型获取数据
	 * @param tClass
	 * @param <T>
	 * @return
	 */
	public <T> T getData (Class<T> tClass) {
		for (Object value : data.values()) {
			if (value.getClass().equals(tClass)) {
				return (T)value;
			}
		}
		return null;
	}

	/**
	 * 根据类型获取数据
	 * @param tClass
	 * @param key
	 * @param <T>
	 * @return
	 */
	public <T> T getData (Class<T> tClass, String key) {
		Object value = data.get(key);
		if (value.getClass().equals(tClass)) {
			return (T)value;
		}
		return null;
	}

	public static void main(String[] args) {
		UserDto user = new UserDto();
		user.getData().put("key", new Role());
		log.info(user.getData(Role.class).toString());
	}
}
